package com.example.pclimb.ui.guides;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.pclimb.R;
import com.example.pclimb.ui.home.PopularGuidesAdapter;
import com.example.pclimb.viewmodel.TravelGuideViewModel;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

public class GuidesFragment extends Fragment {

    private GuidesViewModel mViewModel;
    private SwipeRefreshLayout swipeRefresh;
    private RecyclerView recyclerGuides;
    private TextView textEmpty;
    private FloatingActionButton fabAddGuide;
    private PopularGuidesAdapter adapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_guides, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 初始化视图
        swipeRefresh = view.findViewById(R.id.swipe_refresh);
        recyclerGuides = view.findViewById(R.id.recycler_guides);
        textEmpty = view.findViewById(R.id.text_empty);
        fabAddGuide = view.findViewById(R.id.fab_add_guide);
        
        // 初始化ViewModel
        mViewModel = new ViewModelProvider(this).get(GuidesViewModel.class);
        TravelGuideViewModel travelGuideViewModel = new ViewModelProvider(this).get(TravelGuideViewModel.class);
        
        // 设置RecyclerView
        adapter = new PopularGuidesAdapter();
        recyclerGuides.setLayoutManager(new LinearLayoutManager(requireContext()));
        recyclerGuides.setAdapter(adapter);
        
        // 设置攻略点击事件
        adapter.setOnItemClickListener(guide -> {
            // 跳转到攻略详情页面
            Intent intent = new Intent(requireActivity(), GuideDetailActivity.class);
            intent.putExtra("guide_id", guide.getId());
            startActivity(intent);
        });
        
        // 观察数据变化
        travelGuideViewModel.getAllTravelGuides().observe(getViewLifecycleOwner(), guides -> {
            if (guides != null) {
                adapter.setTravelGuides(guides);
                // 如果没有数据，显示空视图
                if (guides.isEmpty()) {
                    textEmpty.setVisibility(View.VISIBLE);
                    recyclerGuides.setVisibility(View.GONE);
                } else {
                    textEmpty.setVisibility(View.GONE);
                    recyclerGuides.setVisibility(View.VISIBLE);
                }
            }
        });
        
        // 设置下拉刷新
        swipeRefresh.setOnRefreshListener(() -> {
            refreshGuides();
            swipeRefresh.setRefreshing(false);
        });
        
        // 设置添加按钮点击事件
        fabAddGuide.setOnClickListener(v -> {
            // 跳转到创建攻略页面
            Intent intent = new Intent(requireActivity(), CreateGuideActivity.class);
            startActivity(intent);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        // 当Fragment恢复时刷新数据
        refreshGuides();
    }
    
    /**
     * 刷新攻略数据
     */
    private void refreshGuides() {
        TravelGuideViewModel travelGuideViewModel = new ViewModelProvider(this).get(TravelGuideViewModel.class);
        // 通知数据更新了，重新获取
        travelGuideViewModel.refreshAllTravelGuides();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
} 